package com.zhenyi.jiahua.index.question;

import com.jfinal.aop.Clear;
import com.jfinal.kit.LogKit;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.SqlPara;
import com.jiahua.common.model.JhQuestion;
import com.zhenyi.jiahua.index.JHController;

import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;

public class QuestionController extends JHController {

	@Clear
	@Override
	public void index() {
		render("/jh/question/index.html");
	}

	@Clear
	public void questions() {
		String user_cookie = getCookie("user_cookie");
		// System.out.println("user_cookie: " + user_cookie);
		setCookie("user_cookie", "", 0);
		if (StrKit.isBlank(user_cookie)) {
			redirect("/jh/question");
			return;
		}
		if ("undefined".equals(user_cookie)) {
			redirect("/jh/question");
			return;
		}
		if ("null".equals(user_cookie)) {
			redirect("/jh/question");
			return;
		}
		render("/jh/question/questions.html");
	}

	@Clear
	public void testResult() {
		render("/jh/question/testResult.html");
	}

	@Override
	public void list() {
		SqlPara sqlPara = Db.use("jiahua").getSqlPara("JHFindQuestions");
		Page<JhQuestion> pages = JhQuestion.dao.paginate(getPage(), PAGE_SIZE, sqlPara);
		setRainbow(pages);
		setItems(pages.getList());
		render("/jh/question/checkResult.html");
	}

	@Override
	public void info() {
		Long id = getParaToLong("id");
		JhQuestion question = JhQuestion.dao.findById(id);
		setItems(question.getResult());
		setAttr("mark", question.getMark());
		setAttr("comment", question.getComment());
		setAttr("correct", question.getCorrect());
		setAttr("wrong", question.getWrong());
		setAttr("miss", question.getMiss());
		render("/jh/question/info.html");
	}

	@Clear
	@Override
	public void submit() {
		String code = getPara("code", "null");
		if ("null".equals(code)) {
			LogKit.info("no code");
			renderSuccess();
			return;
		}
		String result = getPara("result");
		JSONArray jsonArray = new JSONArray(result);
		Object[] items = jsonArray.toArray();
		int correct = 0;
		int wrong = 0;
		int miss = 0;
		int count1 = 0;
		int count2 = 0;
		int count3 = 0;
		int count4 = 0;
		int count5 = 0;
		int count6 = 0;
		int count7 = 0;
		int count8 = 0;
		int num = 1;
		for (Object object : items) {
			JSONObject jsonObject = new JSONObject(object);
			int isCorrect = jsonObject.getInt("isCorrect", 2);
			if (isCorrect == 0) {
				wrong += 1;
			} else if (isCorrect == 1) {
				correct += 1;
			} else if (isCorrect == 2) {
				miss += 1;
			}
			boolean correctFlag = isCorrect == 1;
			if (!correctFlag) {
				if (num >= 1 && num <= 3) {
					count1++;
				} else if (num >= 4 && num <= 7) {
					count2++;
				} else if (num >= 8 && num <= 14) {
					count3++;
				} else if (num >= 15 && num <= 17) {
					count4++;
				} else if (num >= 18 && num <= 20) {
					count5++;
				} else if (num >= 21 && num <= 28) {
					count6++;
				} else if (num >= 29 && num <= 33) {
					count7++;
				} else if (num >= 34 && num <= 38) {
					count8++;
				}
			}
			num++;
		}
		String cmt = "";
		if (count1 > 0) {
			cmt = cmt + "学生对英语普通生活词汇很欠缺;";
		}
		if (count2 > 2) {
			cmt = cmt + "对应国内4-6级词汇比较欠缺;";
		}
		if (count3 > 4) {
			cmt = cmt + "综合语法欠缺;";
		}
		if (count4 > 2) {
			cmt = cmt + "句子成分的语法知识欠缺;";
		}
		if (count5 > 1) {
			cmt = cmt + "雅思基础阅读能力欠缺;";
		}
		if (count6 > 3) {
			cmt = cmt + "英语基础听力能力欠缺;";
		}
		if (count7 > 1) {
			cmt = cmt + "雅思基础听力能力欠缺;";
		}
		if (count8 > 2) {
			cmt = cmt + "雅思听力理解能力欠缺;";
		}
		String mark = "";
		if (correct > 33) {
			mark = "适合进入雅思冲分班";
		} else if (correct > 15) {
			mark = "英语基础一定程度上适合先进行缺失内容定位和补缺.对应40课时基础课程";
		} else {
			mark = "英语基本素质比较薄弱,需要从基础语法重新构建一遍.词汇欠缺情况比较严重.\n对应课程:20+40课时基础课程";
		}

		JhQuestion question = new JhQuestion();
		question.setCode(code);
		question.setResult(result);
		question.setCorrect(correct);
		question.setWrong(wrong);
		question.setMiss(miss);
		question.setMark(mark);
		question.setComment(cmt);
		question.save();
		renderSuccess();
	}
}
